package model

const ExchangePairEnabled = 1

type exchangePairModel struct {
	Table 		string
}

var ExchangePairModel = &exchangePairModel{
	Table: 		"exchange_pair",
}

// 获取所有交易所下的交易对
func (m *exchangePairModel) Find(mid int) ([]ExchangePair, error) {
	var exchangePairs []ExchangePair
	err := MainDB.Table(m.Table).Where("exchange_mid=?", mid).Find(&exchangePairs)
	if err != nil {
		return nil, err
	}
	return exchangePairs, nil
}

// 获取可用的交易对
func (m *exchangePairModel) GetEnableSymbolsBySlug(slug string) ([]ExchangePair, error) {
	var pairs []ExchangePair
	err := MainDB.Table(m.Table).Where("exchange_slug=? AND enable=?", slug, ExchangePairEnabled).Find(&pairs)
	if err != nil {
		return nil, err
	}
	return pairs, nil
}

// 批量插入
func (m *exchangePairModel) MultiInsert(datas []ExchangePair) (int64, error) {
	return MainDB.Table(m.Table).Insert(datas)
}

// 删除
func (m *exchangePairModel) Remove(datas []ExchangePair) error {
	var data ExchangePair
	ids := make([]int, 0)
	for _, v := range datas {
		ids = append(ids, v.Id)
	}
	_, err := MainDB.Table(m.Table).In("id", ids).Delete(&data)
	return err
}